//var postData = "";
//request.on("data",function(data){
//  postData +=data;
//});
//request.on("end",function(){
//  console.log(postData);
//});

var http = require("http");
var fs = require("fs");
// 异步读取
var buddhaHallData = null;
fs.readFile('BuddhaHallData.json', function (err, data) {
   if (err) {
       return console.error(err);
   }
   buddhaHallData = JSON.parse(data.toString());
// console.log("异步读取: " + data.toString());
});
var demo = http.createServer(function(request,response){
    var path = request.url;
    console.log('收到客户端请求' + path);
    switch(path){
        case "/homepage":
            response.writeHead(200,{"Content-Type":"text/plain"});
            response.write("hello this is homepage");
            response.end();
            break;
        case "/details":
            response.writeHead(200,{"Content-Type":"text/plain"});
            response.write("hello this is details");
            response.end();
            break;
        case "/html":
        	fs.readFile("index.html",function(err,data){
	            response.writeHead(200,{"Content-Type":"text/html; charset=utf-8"});
	            response.write(data);
	            response.end();
	        });
            break;
        case "/buddhaHallData":
            response.writeHead(200,{"Content-Type":"text/plain; charset=utf-8"});
            response.write(JSON.stringify(buddhaHallData));
            response.end();
            break;
        default:
            response.writeHead(404,{"Content-Type":"text/plain"});
            var data = {
            	"wo": "李扬",
            	"g": "5"
            }
            response.write(JSON.stringify(data));
            response.end();
            break;
    }
}).listen("8080");
console.log("server start");